<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use api\home\model\SlideItemModel;
use api\home\model\SlideModel;
use api\user\model\UserBalanceApply;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use app\portal\model\UserBalanceLog;
use app\portal\model\UserModel;
use app\portal\model\UserPortalRelate;
use app\portal\service\PostService;
use cmf\controller\HomeBaseController;
use cmf\controller\RestBaseController;
use cmf\controller\UserBaseController;
use cmf\lib\Upload;
use cmf\phpqrcode\QRcode;
use think\App;
use think\db\Query;
use think\facade\Lang;
use think\File;
use think\file\UploadedFile;
use think\Request;


class AgencyController extends UserBaseController
{
    public function agency()
    {

        $lang = session('Lang');
        if ($lang){
            Lang::setLangSet($lang);
            if ($lang == 'us'){
                $langType = '1';
            }
        }

        $user = cmf_get_current_user();
        $this->assign($user);

        $userId = cmf_get_current_user_id();

        $userModel = new \app\user\model\UserModel();
        $user      = $userModel->where('id', $userId)->find();
        $this->assign('user', $user);

        $is_mobile = cmf_is_mobile();

        // 幻灯片
        $slideModel = new SlideItemModel();
        $where = [];
        $where[] = ['status','=','1'];
        $where[] = ['slide_id','=','2'];
        $slide_list = $slideModel->where($where)->order('list_order desc')->select();


        $domain = $_SERVER['HTTP_HOST'];

// 获取协议
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";

        //  我的连接
        $domain = $protocol.$domain.'?agent='.$userId;


        $this->assign('slide_list', $slide_list);
        $qrTab = QRcode::png($domain, 'upload/default/code/path_to_save_qrcode_'.$userId.'.png', 'L');

        $url = 'default/code/path_to_save_qrcode_'.$userId.'.png';


        // 以邀请列表
        $userList      = $userModel->where('parent_id', $userId)->order('create_time desc')->select();



        $this->assign('imageUrl', $url);
        $this->assign('domain', $domain);
        $this->assign('userList', $userList);
        $this->assign('nav', 'center');


        $this->assign('nav', 'agency');
        $is_mobile = cmf_is_mobile();
        if ($is_mobile == true){
            return $this->fetch(':mobile_agency');
        }else{
            return $this->fetch(':agency');
        }


    }


    public function addPost(Request $request)
    {
        $update = [];
        $param = $this->request->param();

        $upload = new Upload();
        $files = $upload->upload();
        $res = $upload->getError();
        if ($res === false){
            $filepath = $files['filepath'];

            $update['code_icon'] = $filepath;
        }

        $update['user_email'] = $param['user_email'];
        $update['user_wechat'] = $param['user_wechat'];
        $update['user_qq'] = $param['user_qq'];

        $user_id = cmf_get_current_user_id();
        $userModel = new UserModel();
        $info = $userModel->where('id',$user_id)->update($update);

        $is_mobile = cmf_is_mobile();

        $this->redirect('agency');
        exit;

        $this->success('修改成功');
    }

    // 明细页面
    public function agencyInfo(){
        $user_id = cmf_get_current_user_id();

        $userModel = new UserBalanceLog();
        $info = $userModel->where('user_id',$user_id)->order('create_time desc')->select();

        $this->assign('info', $info);

        $is_mobile = cmf_is_mobile();
        if ($is_mobile == true){
            return $this->fetch(':mobile_agency_info');
        }else{
            return $this->fetch(':agency_info');
        }


    }

    // 内转
    public function sendMoney(){
        $param = $this->request->param();
        $type = $param['type'];
        $money = $param['money'];

        $user_id = cmf_get_current_user_id();
        $userModel = new UserModel();
        $info = $userModel->where('id',$user_id)->find();

        if ($money > $info['balance']){
            $this->error('可操作的余额不足');
        }

        $description = '内转';
        if ($type == 1){
            $description = '提现';
        }

        $applyModel = new UserBalanceApply();
        $data = [];
        $data['user_id'] = $user_id;
        $data['change'] = $money;
        $data['balance'] = $info['balance'] - $money;
        $data['description'] = $description;
        $data['type'] = $type;
        $data['status'] = 1;
        $data['create_time'] = time();

        $applyModel->insertGetId($data);

        $this->success('操作成功');

    }





}
